package com.theo.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.theo.entity.HospitalInfo;
import com.theo.entity.StockInfo;
import com.theo.service.CityInfoService;
import com.theo.service.StockInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 方涛
 * @since 2021-02-02
 */
@Controller
@RequestMapping("/stock-info")
public class StockInfoController {

    @Autowired
    private StockInfoService stockInfoService;
    @Autowired
    private CityInfoService cityInfoService;

    @RequestMapping("/show.do")
    public String show(Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 5);
        List<StockInfo> list = stockInfoService.queryAllStockInfo();
        PageInfo pageInfo = new PageInfo(list, 8);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/stock_list";
    }

    @RequestMapping("/input.do")
    public String input(StockInfo stockInfo) {
        stockInfoService.saveStockInfo(stockInfo);
        return "redirect:/stock-info/show.do";
    }

    @RequestMapping("/delete.do")
    public String delete(Integer id) {
        stockInfoService.deleteStockInfo(id);
        return "redirect:/stock-info/show.do";
    }

    @RequestMapping("/toUpdate.do")
    public String toUpdate() {
        return "admin/stock_update";
    }

    @RequestMapping("/update.do")
    public String update(StockInfo stockInfo) {
        stockInfoService.updateSotckInfo(stockInfo);
        return "redirect:/stock-info/show.do";
    }

    @RequestMapping("/updateStatus.do")
    public String updateStatus(Integer id) {
        StockInfo stockInfo = stockInfoService.queryStockInfoById(id);
        int i = stockInfo.getStatus() == 1 ? 0 : 1;
        stockInfo.setStatus(i);
        stockInfoService.updateSotckInfo(stockInfo);
        return "redirect:/stock-info/show.do";
    }
}

